package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.generic.RoundingParams;
import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.bean.BaseRecyclerAdapter;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.basicframework.ui.recyclerview.RecyclerViewIndicator;
import com.thrd.taiexhibition.BaseApplication;
import com.thrd.taiexhibition.adapter.CityHomeAdapter;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.bean.BeanCityHome;
import com.thrd.taiexhibition.bean.CityEvent;
import com.thrd.taiexhibition.bean.HomeRightSkip;
import com.thrd.taiexhibition.bean.HomeSkip;
import com.thrd.taiexhibition.bean.LabelLink;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;
import com.thrd.taiexhibition.presener.CityPresener;
import com.thrd.taiexhibition.ui.LinearLabelView;
import com.thrd.taiexhibition.utils.JumpUtils;
import com.thrd.taiexhibition.view.CityHomeView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 城市首页
 * @author: ${唐杰}
 * @date: 2021/3/10
 */
public class CityHomeFragment extends BaseFragment implements CityHomeView, View.OnClickListener {

    private RecyclerViewIndicator mRecyclerViewIndicator;
    private CityPresener mCityPresener;
    private FrescoImage mFrescoImage1;
    private FrescoImage mFrescoImage2;
    private LinearLabelView mLinearLabelView;
    private CityHomeAdapter cityHomeAdapter;
    private long cityId;
    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_city_home;
    }

    @Override
    protected void setUpView(View mContentView) {
        mRecyclerViewIndicator = mContentView.findViewById(R.id.mRecyclerView);
        LinearLayoutManager manager= new LinearLayoutManager(getMContext());
        manager.setOrientation(RecyclerView.HORIZONTAL);
        mRecyclerViewIndicator.setLayoutManager(manager);
        mFrescoImage1 = mContentView.findViewById(R.id.fi_image_1);
        mFrescoImage2 = mContentView.findViewById(R.id.fi_image_2);
        mLinearLabelView = mContentView.findViewById(R.id.llv_labelview);
        mCityPresener = new CityPresener(this);
    }

    @Override
    protected void setUpData() {
        cityHomeAdapter = new CityHomeAdapter(getMContext(),null);
        mRecyclerViewIndicator.setAdapter(cityHomeAdapter);


        cityHomeAdapter.setItemClick(new BaseRecyclerAdapter.ItemClick<HomeSkip>() {
            @Override
            public void onItemClick(int p, HomeSkip mHomeSkip) {
                skipJump(mHomeSkip.getSkiptype(),mHomeSkip.getOnerelevanceid());
            }
        });

        mFrescoImage1.setOnClickListener(this);
        mFrescoImage2.setOnClickListener(this);
        mLinearLabelView.setOnLinearLabel(new LinearLabelView.OnLinearLabel() {
            @Override
            public void setOnLinearLabel(LabelLink mLinearLabel) {
                skipJump(mLinearLabel.getSkiptype(),mLinearLabel.getOnerelevanceid());
            }
        });
    }

    @Override
    public void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mCityPresener!=null&&mAdvertorialBean==null&&mBaseApplication!=null){
            mCityPresener.getCityHome((int) (cityId= mBaseApplication.getCityId()));
        }else if(mAdvertorialBean!=null){
            onSuccess(mAdvertorialBean);
        }
    }

    @Override
    protected void onRecoveryBundle(Bundle savedInstanceState) {
        super.onRecoveryBundle(savedInstanceState);
        if(savedInstanceState!=null){

            BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
            if(mBaseApplication!=null){
                long cityID=savedInstanceState.getLong("cityId");
                if(mBaseApplication.getCityId()==cityID){
                    int item=savedInstanceState.getInt("Indicator_item",0);
                    mRecyclerViewIndicator.setCurrentItem(item);
                    mAdvertorialBean= (BeanCityHome) savedInstanceState.getSerializable("AdvertorialBean");
                }
            }
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("Indicator_item",mRecyclerViewIndicator.getCurrentItem());
     //   outState.putSerializable("AdvertorialBean",mAdvertorialBean);
        outState.putLong("cityId",cityId);



    }

    @Override
    public void onFragmentPause() {
        super.onFragmentPause();
        if(mRecyclerViewIndicator!=null){
            mRecyclerViewIndicator.onFragmentPause();
        }

    }

    @Override
    public void onFragmentResume() {
        super.onFragmentResume();
        if(mRecyclerViewIndicator!=null){
            mRecyclerViewIndicator.onFragmentResume();
        }
    }


    @Override
    public void onError(String error, int type) {
        getContentView().setState(LoadingState.STATE_ERROR);
    }

    private BeanCityHome mAdvertorialBean;
    @Override
    public void onSuccess(BeanCityHome mAdvertorialBean) {
        this.mAdvertorialBean=mAdvertorialBean;
        if(mAdvertorialBean!=null){

            cityHomeAdapter.refresh(mAdvertorialBean.getHomeSkipList());
            if(mRecyclerViewIndicator.getAdapter()==null){
                mRecyclerViewIndicator.setAdapter(cityHomeAdapter);
            }
            mRecyclerViewIndicator.forceUpdateItemCount();

            if(mAdvertorialBean.getHomeRightSkipList()!=null){
                List<HomeRightSkip> homeRightSkipList =mAdvertorialBean.getHomeRightSkipList();
                String image1= homeRightSkipList.size()>0?homeRightSkipList.get(0).getCityhomerightcoverimg():"";
                FrescoUtils.getInstance().setPrefixProgressLoad(getMContext(),mFrescoImage1,image1, RoundingParams.fromCornersRadius(8));
                String image2= homeRightSkipList.size()>1?homeRightSkipList.get(1).getCityhomerightcoverimg():"";
                FrescoUtils.getInstance().setPrefixProgressLoad(getMContext(),mFrescoImage2,image2, RoundingParams.fromCornersRadius(8));

            }else{
                FrescoUtils.getInstance().setPrefixProgressLoad(getMContext(),mFrescoImage1,"", RoundingParams.fromCornersRadius(8));
                FrescoUtils.getInstance().setPrefixProgressLoad(getMContext(),mFrescoImage2,"",RoundingParams.fromCornersRadius(8));
            }

            mLinearLabelView.setData(mAdvertorialBean.getLabelLinkList());
        }

        getContentView().setState(LoadingState.STATE_SUCCESS);
    }


    @Override
    public void onClick(View v) {
        switch (v. getId()){
            case R.id.fi_image_1:
                if(mAdvertorialBean!=null&&mAdvertorialBean.getHomeRightSkipList()!=null&&mAdvertorialBean.getHomeRightSkipList().size()>0){
                    JumpUtils.JumpUtils(mAdvertorialBean.getHomeRightSkipList().get(0).getSkiptype(),mAdvertorialBean.getHomeRightSkipList().get(0).getOnerelevanceid());
                }
                break;
            case R.id.fi_image_2:
                if(mAdvertorialBean!=null&&mAdvertorialBean.getHomeRightSkipList()!=null&&mAdvertorialBean.getHomeRightSkipList().size()>1){
                    JumpUtils.JumpUtils(mAdvertorialBean.getHomeRightSkipList().get(0).getSkiptype(),mAdvertorialBean.getHomeRightSkipList().get(0).getOnerelevanceid());
                }
                break;
        }

    }


    private void skipJump(int type,int onerelevanceid){

        if(type<3){
            JumpUtils.JumpUtils(type,onerelevanceid);
        }else{  //切换栏目
            CityFragment mCityFragment= (CityFragment) getParentFragment();
            if(mCityFragment!=null){
                mCityFragment.jumpColumn(type);
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdvertorialBean=null;
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchCity(CityEvent mCityEvent){
        if(mCityEvent!=null&&mCityPresener!=null){
            mAdvertorialBean=null;
            mCityPresener.getCityHome((int) (cityId=mCityEvent.getCityId()));
        }

    }
}
